/**
 * @file tellwait1.c
 * @author liangwenhao (1943080020@qq.com)
 * @brief 
 * @version 0.1
 * @date 2021-07-26
 * 
 * @copyright Copyright (c) 2021
 * 
 * @authorGitee  https://gitee.com/liangwenhao
 * @authorGithub https://github.com/WHaoL
 * @authorBlogs https://blog.csdn.net/liangwenhao1108
 *  
 * @Code Description: P197 带有竞争条件的程序，测试父子进程调用顺序
 *                    注意：多次测试，会出现交替/乱序打印 等竞争现象
 *  
 */

#include "apue.h"
static void charatatime(char *str);

int main(void)
{
    pid_t pid;

    if ((pid = fork()) < 0)
    {
        err_sys("fork error");
    }
    else if (pid == 0)
    {
        charatatime("output from child\n");
    }
    else
    {
        charatatime("output from parent\n");
    }
    exit(0);
}

static void charatatime(char *str)
{
    char *ptr;
    int c;
    setbuf(stdout, NULL); // set unbuffered
    for (ptr = str; (c = *ptr++) != 0;)
        putc(c, stdout);
}